﻿//-----------------------------------------------------------------------
// <summary>
//  The file contains a class that used to treat style cop settings 
// </summary>
// <copyright file="PropertyShellRIBootstrapper.cs" company="DSSoftLogic">
//     Copyright@ DSSoftLogic 2011, Non-Commercial. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Property.ShellApplication
{
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.Windows;
    using Microsoft.Practices.Prism.Logging;
    using Microsoft.Practices.Prism.MefExtensions;
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Property.Silverlight.Infrastructure;
    using Property.Silverlight.Module;
    using Property.Silverlight.View;
    using Property.Silverlight.View.Views;

    /// <summary>
    /// This PropertyShellRIBootstrapper is testing the StyleCop rules and how to use them.
    /// http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/08/04/experimenting-with-prism-4-silverlight-mef.aspx
    /// </summary>
    [CLSCompliant(false)]
    public class PropertyShellRIBootstrapper : MefBootstrapper
    {
        /// <summary>
        /// Private Constrant String hold the path of catalog.xaml file.
        /// </summary>
        ////private const string ModuleCatalogUri = "/Property.ShellApplication;component/Catalog.xaml";

        /// <summary>
        /// CallbackLogger will back the application log file.
        /// </summary>
        private readonly CallbackLogger callbackLogger = new CallbackLogger();


        /// <summary>
        /// Create the <see cref="ILoggerFacade"/> used by the PropertyShellRIBootstrapper.
        /// </summary>
        /// <returns>callbackLogger</returns>
        /// <remarks>
        /// The base implementation returns a new TextLogger.
        /// </remarks>
        protected override ILoggerFacade CreateLogger()
        {
            this.Logger = this.callbackLogger;
            return callbackLogger;
        }

        /// <summary>
        /// ConfigureAggregateCatalog method has directly reference to all Module
        /// </summary>
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(AutoPopulateExportedViewsBehavior).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(PropertyShellRIBootstrapper).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NewsArticleView).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(PropertyModule).Assembly));
        }

        /// <summary>
        /// Creates ModuleCatalog <see cref="IModuleCatalog"/> used by Prism.
        /// </summary>
        /// <remarks>
        /// The base implementation returns a new ModuleCatalog.
        /// </remarks>
        /// <returns>
        /// A ConfigurationModuleCatalog.
        /// </returns>
        ////protected override IModuleCatalog CreateModuleCatalog()
        ////{
        ////    return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri(ModuleCatalogUri, UriKind.Relative));
        ////}

        /// <summary>
        /// This is method CreateShell
        /// </summary>
        /// <returns>shell view</returns>
        protected override DependencyObject CreateShell()
        {
            ////ShellView rootShell = new ShellView();
            ////Application.Current.RootVisual = rootShell;
            ////return rootShell;
            return this.Container.GetExportedValue<ShellView>();
        }

        /// <summary>
        /// Override the is InitializeShell 
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.RootVisual = (ShellView)this.Shell;
        }

        /// <summary>
        /// ConfigureDefaultRegionBehaviors method
        /// </summary>
        /// <returns>returns factory</returns>
        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();
            //// Behavior that registers all views decorated with the ViewExport attribute
            factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));
            return factory;
        }

        /// <summary>
        /// Configures the <see cref="CompositionContainer"/>.
        /// May be overwritten in a derived class to add specific type mappings required by the application.
        /// </summary>
        /// <remarks>
        /// The base implementation registers all the types direct instantiated by the bootstrapper with the container.
        /// The base implementation also sets the ServiceLocator provider singleton.
        /// </remarks>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            //// Because we created the CallbackLogger and it needs to be used immediately, we compose it to satisfy any imports it has.
            ////this.Container.ComposeExportedValue<CallbackLogger>(this.callbackLogger);
        }

        // <summary>
        // This is ConfigureModuleCatalog should simply call the all Modules to load it is also use as Module Controller
        // </summary>
        ////protected override void ConfigureModuleCatalog()
        ////{
        ////    // base.ConfigureModuleCatalog();
        ////    ////The ModuleCatalog class is used to define the application's modules from code
        ////    //ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
        ////    //moduleCatalog.AddModule(typeof(PropertyModule));
        ////    Type MD = typeof(PropertyModule);

        ////    this.ModuleCatalog.AddModule(
        ////        new ModuleInfo()
        ////        {
        ////            ModuleName = MD.Name,
        ////            ModuleType = MD.AssemblyQualifiedName,
        ////        }
        ////    );
        ////}
    }
}
